//加载购物车列表
cartlist_load() 
async function cartlist_load(){
  var {data} = await axios.get(carlistAPI,{
      params:{
        id:localStorage.getItem("uid"),
      }
  })
// console.log(data)
  var count_s=0;
  var sum =0;
  var count_z=0;
  var hd =`
  <div class="top">
  <p class="tip">请在下单后 20 分钟内完成支付 [<a href="javascript:;">?</a>]</p>
  <p class="serve"><span class="real">真品防伪码</span><span class="qt">七天无理由退货</span><span
          class="by">美妆满2件或299元包邮</span></p>
</div>
  `
  var html=""
 
 data.data.forEach(function(v,i){
   count_s = parseInt(v.pprice)*parseInt(v.pnum)
   sum = sum+parseInt(v.pnum)
   count_z = count_z+count_s;
     html = html+`
     <table>
                <tr class="t1">
                    <td> <input type="checkbox" checked onchange="slt()"> </td>
                    <td>店铺名</td>
                    <td>聚美价(元)</td>
                    <td>数量</td>
                    <td>小计(元)</td>
                    <td>操作</td>
                </tr>
                <tr class="t2">
                    <td>
                        <input type="checkbox" checked onchange="slt()">
                    </td>
                    <td><img src=${v.pimg} alt="">
                        <p class="desc"><a href="">${v.pdesc}</a><span>${v.pname}</span> </p>
                    </td>
                    <td class="price">${v.pprice}</td>
                    <td>
                        <div class="num"><a href="javascript:;" class="sub" onclick="sub(this,${v.pid})">-</a><input type="text" value="${v.pnum}" onchange="update(this,${v.pid})"><a
                                href="javascript:;" class="add" onclick = "add(this,${v.pid})">+</a>
                        </div>
                    </td>
                    <td>${count_s}</td>
                    <td><a href="javascript:;" onclick="del(${v.pid})">删除</a></td>
                </tr>
                <tr class="t3">
                    <td>商品金额：<span>￥${count_s}</span></td>
                </tr>
            </table>
     `
 })
 var wb = `
 <div class="bot">
 <p class="p1"><input type="checkbox" checked class="select_all" onchange ="selectAll(this)"> 全选<a href="../html/index.html">继续购物</a> <span>|</span><a
         href="javascript:;">清空选中的商品</a></p>
 <p class="p2">共<span class="count">${sum}</span>件商品 商品应付总额: <span class="total">￥${count_z}</span><input type="button"
         value="去结算"></p>
</div>
 `

$(".cart_list .w").html(hd+html+wb)

}
//全选/全不选
function selectAll(r){
    //  console.log(r)
    $("table input[type='checkbox']").prop("checked",r.checked)
    total()
}
//复选框选中、不选中
function slt(){
   var arr = []
$("table input[type='checkbox']").each(function(){
          arr.push($(this).prop("checked"))
       })
      if(arr.every(function(v,i){
          return v==true;
      })){
        $(".bot .select_all").prop("checked",true)
      }else{
        $(".bot .select_all").prop("checked",false)
      }
      total()
}
//更新价格
function total(){
    var ckbs = document.querySelectorAll(".t2 input[type='checkbox']")
    var ckbarr=[...ckbs]
    console.log(ckbarr)
   var arr1 =  ckbarr.filter(function(v,i){
        return v.checked==true
    })
    var arr2 =  ckbarr.filter(function(v,i){
        return v.checked==false
    }) 
    arr2.forEach(function(v,i){
        v.parentNode.parentNode.parentNode.querySelector(".t3 span").innerHTML=0
    })
    var count1 = 0;
    var count2 = 0;
    arr1.forEach(function(v,i){
        count1 = count1+  parseInt(v.parentNode.parentNode.querySelector(".price").innerHTML)*parseInt(v.parentNode.parentNode.querySelector("input[type='text']").value)
        count2 = count2+ parseInt(v.parentNode.parentNode.querySelector("input[type='text']").value)
        v.parentNode.parentNode.parentNode.querySelector(".t3 span").innerHTML=parseInt(v.parentNode.parentNode.querySelector(".price").innerHTML)*parseInt(v.parentNode.parentNode.querySelector("input[type='text']").value)
    })
    $(".bot .total").html("￥"+count1)
    $(".bot .p2 .count").html(count2)
 }

 //加减以及修改商品
 async function add(btn,pid){
    if(parseInt( btn.parentNode.querySelector("input[type='text']").value)>=20){
        btn.parentNode.querySelector("input[type='text']").value=20
    }else{
        btn.parentNode.querySelector("input[type='text']").value = parseInt( btn.parentNode.querySelector("input[type='text']").value)+1;
    }
    await axios.get(updatenumAPI,{
         params:{
             uid:localStorage.getItem("uid"),
             pid:pid,
             pnum:btn.parentNode.querySelector("input[type='text']").value
         }
     })
     total()  
}
async function sub(btn,pid){
    if(parseInt( btn.parentNode.querySelector("input[type='text']").value)<=1){
        btn.parentNode.querySelector("input[type='text']").value=1
    }else{
        btn.parentNode.querySelector("input[type='text']").value = parseInt( btn.parentNode.querySelector("input[type='text']").value)-1;
    }
   await axios.get(updatenumAPI,{
        params:{
            uid:localStorage.getItem("uid"),
            pid:pid,
            pnum:btn.parentNode.querySelector("input[type='text']").value
        }
    })
    total()
}
async function del(pid){
    var flag = confirm("你确定要删除此商品吗?")
    if(flag){
        await axios.get(delcarAPI,{
            params:{
                uid:localStorage.getItem("uid"),
                pid:pid,
            }
        })
    }else{

    }
    cartlist_load()
   total()
}
async function update(btn,pid){
    if(parseInt( btn.parentNode.querySelector("input[type='text']").value)<=1){
        btn.parentNode.querySelector("input[type='text']").value=1
    }else if(parseInt( btn.parentNode.querySelector("input[type='text']").value)>=20){
        btn.parentNode.querySelector("input[type='text']").value=20
    }
   await axios.get(updatenumAPI,{
        params:{
            uid:localStorage.getItem("uid"),
            pid:pid,
            pnum:btn.parentNode.querySelector("input[type='text']").value
        }
    })
    total()
}

